const { chromium } = require('playwright');//引用模拟浏览器
const { loginX } = require('./loginX')
const { change_username, change_password, change_tow_factor, change_profile } = require('./XQuikUrl')
const { getEmail } = require('../outlook/getEmail')
// AgustinM28339:X4HQHtPdLc8b:dazsujywqo483@outlook.com:X4HQHtPdLc8b:cafb0befda49e9f91cf9f8c12dfb108057414da5:2fa.fb.rip/4WBS675EOSS64XA4

const ChangeUsername = async (page)=>{
    await page.goto(change_username, {timeout:120000})
    await page.waitForTimeout(5000)
    let username = await page.locator('xpath=//span[text()="Suggestions"]/ancestor::*[position()=4]/div/button/span').first().innerText();
    username = username.slice(0,5) + Math.floor(Math.random() * 100000) + 1;
    await page.getByRole('textbox', { name: 'Username' }).fill(username);
    await page.getByTestId('settingsDetailSave').click()
    return username
}

const changePassword = async (page, pwd, new_pwd)=>{
    await page.goto(change_password, {timeout:120000})
    await page.waitForTimeout(5000)
    await page.getByRole('textbox', { name: 'Current password' }).click();
    await page.getByRole('textbox', { name: 'Current password' }).fill(pwd);
    await page.getByRole('textbox', { name: 'New password' }).click();
    await page.getByRole('textbox', { name: 'New password' }).fill(new_pwd);
    await page.getByRole('textbox', { name: 'Confirm password' }).click();
    await page.getByRole('textbox', { name: 'Confirm password' }).fill(new_pwd);
    await page.getByTestId('settingsDetailSave').click()
}


const changeTwoFactor = async (page, pwd)=>{
    const oprate = async (page)=>{
        await page.goto(change_tow_factor, {timeout:120000})
        await page.waitForTimeout(5000)
        await page.locator("xpath=//span[text()='Authentication app']").click();
        await page.waitForTimeout(5000)
        await page.locator("xpath=//input[@name='password']").click()
        await page.locator("xpath=//input[@name='password']").fill(pwd);
        await page.getByTestId('LoginForm_Login_Button').click()
        await page.waitForTimeout(5000)
    }
    await oprate(page)
    const turn_off =  await page.getByTestId('OCF_CallToAction_Button').count() > 0;
    if (turn_off){
        await oprate(page)
        await page.locator('xpath=span[@text()="Send code"]').click();
        const code = await getEmail(page)
        await page.getByRole('textbox', { name: 'Enter the verification code' }).fill(code);
        await page.getByRole('button', { name: 'Confirm' }).click()
    }
}

const changeProfile = async (page)=>{
    await page.goto(change_profile, {timeout:120000})
    await page.waitForTimeout(5000)
    // await page.getByRole('button', { name: 'Add photos or video' }).click();
    await page.setInputFiles('input[type="file"]', 'C:/Users/明达/Pictures/2.webp');
    await page.locator('xpath=//span[text()="Skip for now"]').click();
    await page.locator('xpath=//span[text()="Skip for now"]').click();
    await page.locator('xpath=//span[text()="Skip for now"]').click();
    await page.locator('xpath=//span[text()="Save"]').click();
}


const initX = async (browser) => {
    const user = 'AgustinM28339'; // 替换为你的用户名或邮箱地址，用于登录和验证邮箱
    const pwd = 'X4HQHtPdLc8b';
    const email = 'dazsujywqo483@outlook.com'
    const new_pwd = 'shufa123'
    const tow_fa_url = '2fa.fb.rip/4WBS675EOSS64XA4'
    // const browser = await chromium.launch({ headless: false, slowMo:50});
    // const page = await browser.newPage();
    const page = await browser.newPage();
    await page.goto('https:www.x.com', { timeout: 500000 });
    await page.waitForTimeout(5000);
    await loginX(browser, page, user, new_pwd, email, tow_fa_url)
    await changeProfile(page);
    // await changeTwoFactor(page, pwd);
    // const new_username = await ChangeUsername(page)
    // console.log(new_username)
    // await changePassword(page, pwd, new_pwd)
}

module.exports = { initX };